#ifndef MAIN_H
#define MAIN_H
#include <QObject>

#include "ui_machine_manage.h"
#include "ui_manual_operation.h"
#include "ui_sterilization_type.h"
#include "ui_sterilizing_runing.h"
#include "ui_TP_meter.h"
#include "ui_time_display.h"
#include "ui_top_menu.h"
#include "ui_warn.h"
#include "ui_welcom.h"
#include "ui_password_input.h"
#include "gpio.h"

#include "data_acquisation_serial.h"
#include "manual_operation.h"
#include "monitor.h"
#include "password_input.h"
#include "sterilization_setting.h"
#include "sterilization_type.h"
#include "sterilizing_runing.h"
#include "TP_meter.h"
#include "time_display.h"
#include "top_menu.h"
#include "warn.h"
#include "welcom.h"
#include "recorder.h"
#include "pwm.h"
#include "xml_core.h"
#include "leak_test.h"
#include "bottom_setting.h"
#include "printer.h"
#include "machine_type.h"
#include "door_operations.h"
#include "progressdialog.h"
#include "special_setting.h"
#include "docreader.h"
#include "mythread.h"
#include "sterilization_config_data.h"
#include "mvcd.h"
#include "machine_parameter.h"
#include "tracker.h"


#define ADC_INPUT_PIN   _IOW('S', 0x0c, unsigned long)
#define button_background_color_red     "background-color: rgb(255, 0, 0)"
#define button_background_color_green     "background-color: rgb(0,255, 0)"
#define button_background_color_blue    "background-color: rgb(0,0, 255)"
#define button_background_color_gray     "background-color: rgb(170,170, 127)"

#define BUTTON_SOUND             pwm->pwm_tweet(100);

enum protype{
  NO_CHOICE,
 TEXTITLES_PROTYPE ,
 BD_TEST_PROTYPE ,
 METALS_PROTYPE ,
 BOTTOM_EXHAUST_PROTYPE ,
 LIQUID_RPOTYPE,
 MANUAL_OPERATION,
 DRY_PROTYPE,
 LEAK_TEST_PROTYPE,
    USER_DEFINE_1,
    USER_DEFINE_2,
    USER_DEFINE_3
} ;
enum error_status{
 NO_ERROR      ,
 LOW_TEM       ,
 Vacuum_pump_NO_WATER  ,
 Vacuum_pump_TimeOut,
 LOWEST_WATER  ,
 DOOR_ERROR    ,
 OVERPRESSURE,
 MANUAL_STOP,
 PRESSURE_REACHED,
    OVERTIME
};
enum device_type{
    skip_first_zero            ,
    square_auto_double_door    ,
    square_auto_single_door    ,
    square_manual_double_door  ,
    square_manual_single_door  ,
    circular_double_door       ,
    circular_single_door
} ;
enum door_action{
 no_action                ,
 openning_front_door     ,
 closing_front_door      ,
 openning_back_door   ,
 closing_back_door     ,
 halting_door
};

enum waterlevel{
    null,
    leak,//没有水源
    low,//低于最低水位
    normal,
    high
};

class MyMessageBox : public QMessageBox {

public:
    explicit MyMessageBox()
    {
       setWindowTitle(trUtf8("注意"));
       buttonyes=new QPushButton(trUtf8("确认"));
       buttonno=new QPushButton(trUtf8("取消"));
       buttonyes->setFixedSize(100,60);
       buttonno->setFixedSize(100,60);

       addButton(buttonyes,QMessageBox::AcceptRole);
       addButton(buttonno,QMessageBox::RejectRole);
    }

   bool question(const char* ch)
    {
     setInformativeText(trUtf8(ch));
     //return( exec()==QDialog::Accepted) ;
     exec();
     return(clickedButton()==buttonyes);
    }

protected:
    void showEvent(QShowEvent* event)
    {
        QMessageBox::showEvent(event);
        setFixedSize(400, 200);
    }

private:
    QPushButton * buttonyes;
    QPushButton * buttonno;
};


extern QMap<QString,int >  textiles_datamap;
extern QMap<QString,int >  metals_datamap;
extern QMap<QString,int >  bottom_datamap;
extern QMap<QString,int >  liquid_datamap;
extern QMap<QString,int >  user_define1_datamap;
extern QMap<QString,int >  user_define2_datamap;
extern QMap<QString,int >  user_define3_datamap;
extern QMap<QString,int >  sensor_adjust_datamap;
extern QMap<QString,int >  IO_setting_datamap;
extern waterlevel  water;
extern protype sterilization_protype;
extern door_action door_actions;
extern error_status breakdown;

#define STOP_ACTION      {Vacuum_pump_OFF;air_intake_valve_OFF;LEDA_OFF;\
    exhaust_valve_OFF;air_supply_valve_OFF;}//heating_valve_OFF;
#define STOP_CMD         if(sterilizing){controller->stop();  }
#define _EXIT_0          if(this->stop_thread) {this->stop_thread=0;sterilizing=0;STOP_ACTION return 0;}
#define _EXIT_          if(this->stop_thread) {this->stop_thread=0;sterilizing=0;STOP_ACTION return;}


#define LOG_DOC             "/qt/log"
#define LOCAL_PATH          "/qt/sterilization/"
#define RECORD_DIR          "/qt/sterilization/history/"
#define IODATA_RECORD_DIR   "/qt/sterilization/Iodata/"
#define RECORD_DOC          "/qt/sterilization/doc"
#define EXECUTABLE_FILE     "/qt/sterilization/app"
#define REMARK_FILE         "/qt/sterilization/readme.txt"
#define HELP_FILE           "/qt/sterilization/help.txt"
#define DATA_FILE           "/qt/sterilization/config.dat"
#define UPDATEURL_FILE      "/qt/sterilization/UpdateUrl.txt"
#define SD_PATH             "/sdcard/app"
#define manage_password     "120"
#define supper_password     "110"
#define COM3 "/dev/ttySAC2"
#define COM4 "/dev/ttySAC3"
#define Sealing_strip_Exhaust   1
#define Door_Actions            2
#define Open_Door               3
#define Close_Door              4
#define door_action_delay_ms    1000
#define door_action_time_ms     30000
#define door_Sealing_strip_Exhaust_time_ms    10000

#define MAX_OPEN_DOOR_PRESSURE       5
#define MIN_OPEN_DOOR_PRESSURE      -9

//#define DISPLAY(n)        sr->status_changed=1;sterilization_stage=n;
//#define SET_TIMER(n)      sr->timer_working=0;sr->timer_count=n;

#define SET_COLOR_GREEN     setStyleSheet(button_background_color_green);
#define SET_COLOR_RED       setStyleSheet(button_background_color_red);
#define SET_COLOR_BLUE      setStyleSheet(button_background_color_blue);
#define SET_COLOR_GRAY      setStyleSheet(button_background_color_gray);

//#define Affirm(text)    QMessageBox::question(this,trUtf8("注意！"),trUtf8(text),\
//                          QMessageBox::Ok|QMessageBox::Cancel)==QMessageBox::Ok

#define Affirm(text)    messagebox->question(text)

#define START_AUTO_HEAT  {heating_valve_ON auto_heating=true;}
#define STOP_AUTO_HEAT  {auto_heating=false;heating_valve_OFF}


extern void GPIO_SETUP();
extern void COM_SETUP(int &fd,const char* tty);
extern int  sensor_test();
extern void certificate();
extern void read_config(QString strTemp);
extern void save_config(QString strTemp);
extern void file_clean(const char* dirpath);
extern void sterilization_default_setting_recovery(QMap<QString,int >& datamap,uint checktype);

extern int water_valve_pin;
extern int heating_valve_pin;
extern int air_intake_valve_pin;
extern int exhaust_valve_pin;
extern int Vacuum_pump_pin;
extern int air_supply_valve_pin;
extern int open_front_door_pin;
extern int close_front_door_pin;
extern int front_door_Sealing_strip_Inflation_pin;
extern int front_door_Sealing_strip_Exhaust_pin;
extern int open_back_door_pin;
extern int close_back_door_pin;
extern int back_door_Sealing_strip_Inflation_pin;
extern int back_door_Sealing_strip_Exhaust_pin;
extern int sterilization_temperature;
extern int sterilization_time;
extern int pulsing_times;
extern int pulsing_lowest_pressure;
extern int pulsing_highest_pressure;
extern int prebalance_time;
extern int dry_pressure;
extern int dry_keep_time;
extern int dry_times;
extern int cold_exhaust_pressure;
extern int cold_exhaust_times;
extern int heat_delay_time;
extern int recdnr;
extern int PSU;
extern int sterilization_stage;
extern int ad_value[8];
//extern int cmd_code;
extern uint vacuum_door_Sealing_strip_Exhaust_time_ms;
extern uint bottom_door_Sealing_strip_Exhaust_time_ms;

extern char AD_VALUE[8][8];

extern float  TEM;
extern float reference_f0;
extern bool workable;
extern bool Vacuum_pump_workable;
extern bool sterilizing;
extern bool water_valve_isopen;
extern bool water_supply_isopen;
extern bool heating_valve_isopen;
extern bool Vacuum_pump_isopen;
extern bool air_supply_valve_isopen;
extern bool air_intake_valve_isopen;
extern bool exhaust_valve_isopen;
extern bool door_closed_OK;
extern bool workDone;
extern bool continuous_mode;
extern bool test_mode;
extern bool config_changed;
extern bool auto_heating;
extern bool sensor_data_changed;
//extern bool IO_drive_type;
//extern bool cmd_action;
extern bool bottom_water_level_isreached;//下水位
extern bool top_water_level_isreached;//上水位
extern bool water_switch_isopen;//GPC0//水流开关
extern bool inter_pressure_switch_isreached;//夹层压力开关
extern bool front_door_closed_in_place;//GPC2
extern bool front_door_opened_in_place;//GPC3
extern bool front_door_Sealing_strip_pressure_in_place;// GPN6
extern bool back_door_opened_in_place;//GPN9
extern bool back_door_closed_in_place;//GPN11
extern bool back_door_Sealing_strip_pressure_in_place;// GPL8
extern bool overpressure_protection_switch_isreached;
extern bool water_level_failure;
extern bool backdoor_canopen;
extern bool door_detection_failure;

extern QString str_sterilization_type;
//extern QMutex mutex;
extern QPalette redtextpal;
extern QPalette blacktextpal;
extern QPalette greentextpal;
extern QPalette bluetextpal;
extern QPair<int,bool> cmd_code_pair;
extern QQueue<QPair<int,bool> > command_queue;
extern QTextBrowser* textbrowser;
//extern QTimer delay_heat_timer;

extern PWM* pwm;
extern monitor* monitor_program;
extern MVCD* MCD_Interface;
extern door_operations* door_operation;
extern warn* WARN;
extern Password_input* input;
extern printer* serialprinter;
extern mythread* controller;
extern sterilization_type* stype;
extern machine_parameter whole_machine_parameter;
extern MyMessageBox* messagebox;
extern tracker* track;
extern data_acquisation* AD_data;

#endif // MAIN_H
